package test.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
import org.eclipse.jface.text.*;

import java.text.*;
import java.awt.font.*;
import org.eclipse.swt.custom.StyleRange;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.font.TextAttribute;
/**
 * Our sample handler extends AbstractHandler, an IHandler base class.
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
public class SampleHandler extends AbstractHandler {
	/**
	 * The constructor.
	 */
	public SampleHandler() {
	}

	/**
	 * the command has been executed, so extract extract the needed information
	 * from the application context.
	 */
	
    
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		
		IWorkbenchWindow currentWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		IWorkbenchPage activePage = window.getActivePage(); // null check omitted
		IEditorPart editorPart = activePage.getActiveEditor(); // null check omitted
		ITextEditor textEditor = (ITextEditor) editorPart; // casting check omitted
		IDocument currentDocument = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
        
		int length = 0;
		length = currentDocument.getLength();
		String tofind = "window";
		
		FindReplaceDocumentAdapter documentfinder = new FindReplaceDocumentAdapter(currentDocument);
		int start = 0;
		int counter = 0;
		
		while (true)
		{
							try {
			                              
			                             IRegion region = documentfinder
			                                        .find(start, tofind,
			                                                true /* forwardSearch */,
			                                                true /* caseSensitive */,
			                                               true /* wholeWord */, false /* regExSearch */);
			                         
			                             ((ITextEditor) textEditor).selectAndReveal(region.getOffset(), region.getLength());
			                             start = region.getOffset() + region.getLength();
			                             
			                            }
										catch (BadLocationException e) {
			                            	e.printStackTrace();
			                            }
			              counter ++;              
		}    
		
		
		
		/*String myString = "";
		myString = Integer.toString(length);
		
		MessageDialog.openInformation(
				window.getShell(),
				"Test",
				myString);
		*/
		//return null;
	}
}
